﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using ImageStoreNET.Developer.WebControls;
using System.Web.UI.WebControls;
using ImageStoreNET.Developer.Core;
using System.Collections;

namespace EPiServer.Municipality.Web.UI.WebControls
{
    public class IVPagedFileList : IVFileListData, INamingContainer
    {
        /* *******************************************************************
         *  Properties 
         * *******************************************************************/
        #region public ITemplate HeaderTemplate
        /// <summary>
        /// Get/Sets the HeaderTemplate of the PagedNewsList
        /// </summary>
        /// <value>
        /// The header template.
        /// </value>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof(IVFileTemplateContainer))]
        public ITemplate HeaderTemplate
        {
            get
            {
                return _headerTemplate;
            }
            set
            {
                _headerTemplate = value;
            }
        }
        private ITemplate _headerTemplate;
        #endregion
        #region public ITemplate ItemTemplate
        /// <summary>
        /// Get/Sets the ItemTemplate of the PagedNewsList
        /// </summary>
        /// <value>
        /// The item template.
        /// </value>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof(IVFileTemplateContainer))]
        public ITemplate ItemTemplate
        {
            get
            {
                return _itemTemplate;
            }
            set
            {
                _itemTemplate = value;
            }
        }
        private ITemplate _itemTemplate;
        #endregion
        #region public ITemplate EmptyItemTemplate
        /// <summary>
        /// Get/Sets the EmptyItemTemplate of the PagedNewsList
        /// </summary>
        /// <value>
        /// The empty item template.
        /// </value>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof(IVFileTemplateContainer))]
        public ITemplate EmptyItemTemplate
        {
            get
            {
                return _emptyItemTemplate;
            }
            set
            {
                _emptyItemTemplate = value;
            }
        }
        private ITemplate _emptyItemTemplate;
        #endregion
        #region public ITemplate FooterTemplate
        /// <summary>
        /// Get/Sets the FooterTemplate of the PagedNewsList
        /// </summary>
        /// <value>
        /// The footer template.
        /// </value>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof(IVFileTemplateContainer))]
        public ITemplate FooterTemplate
        {
            get
            {
                return _footerTemplate;
            }
            set
            {
                _footerTemplate = value;
            }
        }
        private ITemplate _footerTemplate;
        #endregion
        #region public int PageSize
        /// <summary>
        /// Get/Sets the PageSize of the PagedNewsList
        /// </summary>
        /// <value>
        /// The size of the page.
        /// </value>
        public int PageSize
        {
            get { return _pageSize; }
            set { _pageSize = value; }
        }
        private int _pageSize;
        #endregion
        #region public string ResultType
        /// <summary>
        /// Get/Sets the ResultType of the PagedList
        /// </summary>
        /// <value>
        /// The type of the result.
        /// </value>
        public string ResultType
        {
            get { return _resultType; }
            set { _resultType = value; }
        }
        private string _resultType;
        #endregion
        #region public PagedDataSource PagedSource
        /// <summary>
        /// Get/Sets the PagedSource of the PagedNewsList
        /// </summary>
        public PagedDataSource PagedSource
        {
            get
            {
                if (_pagedSource == null)
                {
                    _pagedSource = new PagedDataSource
                    {
                        DataSource = GetFiles(),
                        AllowPaging = true,
                        PageSize = _pageSize
                    };
                }
                return _pagedSource;
            }
        }
        private PagedDataSource _pagedSource;
        #endregion
        #region private int CurrentPageIndex
        /// <summary>
        /// Gets the CurrentPageIndex of the NewsList
        /// </summary>
        /// <value>
        /// The index of the current page.
        /// </value>
        private int CurrentPageIndex
        {
            get
            {
                if (HttpContext.Current.Request.QueryString["page"] != null)
                {
                    int index = (Convert.ToInt32(HttpContext.Current.Request.QueryString["page"]) - 1);
                    // correct for a page index less than 0 or greater then last page
                    if (index < 0)
                        index = 0;
                    if (index > PagedSource.PageCount)
                        index = (PagedSource.PageCount - 1);
                    return index;
                }
                return 0;
            }
        }
        #endregion
        #region public new PagingControl PagingControl
        /// <summary>
        /// Gets the PagingControl of the PagedNewsList
        /// </summary>
        public PagingControl PagingControl
        {
            get
            {
                if (_pagingControl == null)
                    _pagingControl = new PagingControl(PagedSource) { ResultText = ResultType };
                return _pagingControl;
            }
        }
        private PagingControl _pagingControl;
        #endregion
        /* *******************************************************************
         *  Methods 
         * *******************************************************************/
        #region public override void DataBind()
        /// <summary>
        /// Datas the bind.
        /// </summary>
        public override void DataBind()
        {
            base.OnDataBinding(EventArgs.Empty);
            this.Controls.Clear();
            base.ClearChildViewState();
            base.ChildControlsCreated = false;
            this.CreateChildControls();
            base.ChildControlsCreated = true;
            this.TrackViewState();
            for (int i = 0; i < base.Controls.Count; i++)
            {
                base.Controls[i].DataBind();
            }

        }
        #endregion
        #region protected override void CreateChildControls()
        /// <summary>
        /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.
        /// </summary>
        protected override void CreateChildControls()
        {
            PagedSource.CurrentPageIndex = CurrentPageIndex;

            IEnumerator e = PagedSource.GetEnumerator();

            IVFileDataCollection files = new IVFileDataCollection();

            while (e.MoveNext())
            {
                files.Add(e.Current as IVFileData);
            }

            Control template = new IVFileTemplateContainer(null);
            // Abort if no data
            if (files.Count == 0)
            {
                if (_emptyItemTemplate != null)
                {
                    _emptyItemTemplate.InstantiateIn(template);
                    Controls.Add(template);
                }
                return;
            }

            template = new IVFileTemplateContainer(null);
            if (_headerTemplate != null)
            {
                _headerTemplate.InstantiateIn(template);
                Controls.Add(template);
            }

            for (int i = 0; i < files.Count; i++)
            {
                template = new IVFileTemplateContainer(files[i]);
                if (_itemTemplate != null)
                {
                    _itemTemplate.InstantiateIn(template);
                    Controls.Add(template);
                }
            }
            if (_footerTemplate != null)
            {
                template = new IVFileTemplateContainer(null);
                _footerTemplate.InstantiateIn(template);
                Controls.Add(template);
            }

            // Add paginator
            Controls.Add(PagingControl);
        }
        #endregion
        /* *******************************************************************
         *  Event methods 
         * *******************************************************************/
    }
}